import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import DiscoverView from '../views/DiscoverView.vue'
import OrdersView from '../views/OrdersView.vue'
import ProfileView from '../views/ProfileView.vue'
import LoginView from '../views/LoginView.vue'
import Mainlayout from '@/views/Mainlayout.vue'
import StoreListView from '../views/StoreListView.vue'
import Payment from '../views/PayView.vue'
import StoreInfomationView from '@/views/StoreInfomationView.vue'
import MembershipView from '@/views/MembershipView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path:'/storeinformation',
      component: StoreInfomationView,
      meta:{
        requiresAuth: true
      }
    },
    {
      path: '/membership',
      name: 'membership',
      component: MembershipView,
      meta: {
        requiresAuth: true
      }
    },
    {
      path: '/',
      component: Mainlayout,
      children: [
        {
          path: '',
          name: 'home',
          component: HomeView,
          meta: {
            requiresAuth: true
          }
        },
        {
          path: 'discover',
          name: 'discover',
          component: DiscoverView,
          meta: {
            requiresAuth: true
          }
        },
        {
          path: 'orders',
          name: 'orders',
          component: OrdersView,
          meta: {
            requiresAuth: true
          }
        },
        {
          path: 'profile',
          name: 'profile',
          component: ProfileView,
          meta: {
            requiresAuth: true
          }
        },
        {
          path:'storelist',
          name:'storelist',
          component:StoreListView,
          meta: {
            requiresAuth: true
          }
        },
        {
          path:'payment',
          name:'payment',
          component:Payment,
          meta: {
            requiresAuth: true
          }
        },
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: LoginView,
      meta: {
        requiresAuth: false
      }
    }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem('token') // 这里可以根据实际的认证方式修改

  if (to.meta.requiresAuth && !isAuthenticated) {
    next('/login')
  } else {
    next()
  }
})

export default router
